/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HAOJianguo;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import longprojectihm.ContactEditorGUI;
import model_package.Case;
import model_package.Log;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author HAO Jianguo
 */
public class LogToMemory {

    public static Object[][] logToMemoryAndDisplay(String filePath) {
        Object[][] data = null;
        int eventIDGen = 0;
        Log log = ContactEditorGUI.getLog();
        int line = 0;
        try {
            XMLWriter writer = null;
            SAXReader reader = new SAXReader();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");

            File file = new File(filePath);
            if (file.exists()) {
                Document document = reader.read(file);
                Element root = document.getRootElement();
                boolean bl = false;
                // Elements of root - case
                for (Iterator i = root.elementIterator("case"); i.hasNext();) {
                    // Object case
                    Element _case = (Element) i.next();
                    // Case name
                    String caseName = _case.attributeValue("name");
                    // Case Element - event
                    for (Iterator j = _case.elementIterator("event"); j.hasNext();) {
                        Element event = (Element) j.next();

                        //String eventName = event.attributeValue("name");
                        for (Iterator k = event.elementIterator("receivers"); k.hasNext();) {
                            Element receivers = (Element) k.next();
                            for (Iterator l = receivers.elementIterator("receiver"); l.hasNext();) {
                                Element receiver = (Element) l.next();
                                line++;
                            }
                        }
                    }
                }
                data = new Object[line][8];
                line = 0;
                for (Iterator i = root.elementIterator("case"); i.hasNext();) {
                    // Object case
                    Element _case = (Element) i.next();
                    // Case name
                    String caseName = _case.attributeValue("name");
                    Case caseObjet = new Case(caseName);
                    // Case Element - event
                    for (Iterator j = _case.elementIterator("event"); j.hasNext();) {
                        Element event = (Element) j.next();
                        ++eventIDGen;
                        String taskName = event.element("task").attributeValue("name");
                        String performatifName = event.element("performatif").attributeValue("name");
                        String actorName = event.element("actor").attributeValue("name");
                        String conversationID = event.element("conversation").attributeValue("id");
                        String dateDate = event.element("date").attributeValue("date");
                        ArrayList<String> listReceviers = new ArrayList<String>();

                        for (Iterator k = event.elementIterator("receivers"); k.hasNext();) {
                            Element receivers = (Element) k.next();
                            for (Iterator l = receivers.elementIterator("receiver"); l.hasNext();) {
                                Element receiver = (Element) l.next();
                                String receiverName = receiver.attributeValue("name");
                                listReceviers.add(receiverName);
                                data[line][0] = caseName;
                                data[line][1] = eventIDGen;
                                data[line][2] = taskName;
                                data[line][3] = performatifName;
                                data[line][4] = actorName;
                                data[line][5] = receiverName;
                                data[line][6] = conversationID;
                                data[line][7] = dateDate;
                                line++;
                            }
                        }
                        log.addEvent(caseName, taskName, actorName, listReceviers, performatifName, conversationID, dateFormatTest(dateDate));
                    }
                    log.addCase(caseObjet);
                    log.addCasesID(caseName);
                }
            }
        } catch (Exception e) {
        }
        log.toString("T");
        return data;
    }

    public static Date dateFormatTest(String dateString) throws ParseException {
        //dateString = "16/02/2012 11:18"; 
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        Date convertedDate = dateFormat.parse(dateString);
        return convertedDate;
    }
}
